//
//  IfengOrderStore.m
//  IfengNews
//
//  Created by Ryan on 13-7-12.
//
//

#import "IfengOrderStore.h"
#import "CLGloble.h"
#import "NSObject+sagittarius.h"

//#define IfengOrderStore_orderStore @"Ifeng_index_menu_selected4.2.0"

#define IfengOrderStore_orderStore @"chanlinmenu20151110"



@interface IfengOrderStore ()
@property (nonatomic,strong)NSArray *menuList;

@end


@implementation IfengOrderStore

SGR_DEF_SINGLETION(IfengOrderStore)

- (void)setMenu:(NSDictionary *)dic atIndex:(int)index{
    NSArray *arr=[self getOrderedMenuItem];
    for(NSDictionary *adic in arr){
        if([[dic sgrGetStringForKey:@"channelId"] isEqualToString:[adic sgrGetStringForKey:@"channelId"]]){
            return;
        }
    }
    
    NSMutableArray *mutab=[NSMutableArray arrayWithArray:arr];
    [mutab sgrInsertObject:dic atIndex:1];
    [self setOrderMenuItem:mutab];
}

- (BOOL)removeMenuForChannelId:(NSString *)channelId{
    
    BOOL b=NO;
    NSMutableArray *mutab=[NSMutableArray array];
    NSArray *arr=[self getOrderedMenuItem];
    for(NSDictionary *adic in arr){
        if([channelId isEqualToString:[adic sgrGetStringForKey:@"channelID"]]){
            b=YES;
            continue;
        }
        [mutab sgrAddObject:adic];
    }
    [self setOrderMenuItem:mutab];
    return b;
    
}


- (NSArray *)menuList:(NSArray *)items{
    NSMutableArray *mutableArray=[NSMutableArray array];
    int n=0;
    n=[items count];
    NSMutableSet *set=[[NSMutableSet alloc] init];
    for(int i=0;i<n;i++){
        NSDictionary *dic=[[CLGloble sharedInstance].menus
                           sgrGetDictionaryForKey:[items sgrGetStringForIndex:i] ];
        
        if(dic && ![set containsObject:[items sgrGetStringForIndex:i]]){
            
            [set addObject:[items sgrGetStringForIndex:i]];
            [mutableArray sgrAddObject:dic];}
    }
    return mutableArray;
}

- (NSArray *)getUnOrderMenuItem{
    NSMutableArray *mutableArray=[NSMutableArray array];
    NSArray *keys=[[CLGloble sharedInstance]  allKeys];
    int n=0;
    n=[keys count];
    
    NSDictionary *set=[self orderIdSet];
    
    for(int i=0;i<n;i++){
        NSDictionary *dic=[[CLGloble sharedInstance].menus
                           sgrGetDictionaryForKey:[keys sgrGetStringForIndex:i]];

        if([set objectForKey:[dic sgrFGetStringForKey:@"channelId"]]){
            continue;
        }
        [mutableArray addObject:dic];
    }
    
    return mutableArray;
}

- (NSDictionary *)orderIdSet{
    NSArray *array=[self getOrderedMenuItem];
    NSMutableDictionary *set=[[NSMutableDictionary alloc] init];
    for(NSDictionary *dic in array){
        NSString *str=[dic sgrFGetStringForKey:@"channelId"];
        if(_isStrNotNull(str))
        [set setObject:str forKey:str];
    }
    return set;
}


- (NSArray *)getOrderedMenuItem{
    if(self.menuList && [self.menuList count]>0)return self.menuList;
    NSArray *selectedMenu=[self getStroe];
    if(!selectedMenu){
        self.menuList=[self getDefaultOrderMenuItem];
    }else{
        self.menuList=[self menuList:selectedMenu];
    }
    
    return self.menuList;
}

- (NSArray *)getDefaultOrderMenuItem{
    NSArray *array=[CLGloble sharedInstance].defaultOrderMenuItems;
    return [self menuList:array];
}

- (void )store:(NSArray *)array{
    [[NSUserDefaults standardUserDefaults] setObject:array
                                              forKey:IfengOrderStore_orderStore];
   
    [[NSUserDefaults standardUserDefaults] synchronize];

}

- (NSArray *)getStroe{
    NSArray *orderstor=  [[NSUserDefaults standardUserDefaults] arrayForKey:IfengOrderStore_orderStore];
    if(orderstor && [orderstor count]>0){
        if([@"toutiao" isEqualToString:[orderstor sgrGetStringForIndex:0]]){
            NSMutableArray *newArray=[NSMutableArray arrayWithCapacity:[orderstor count]];
            for(NSString *key in orderstor){
                [newArray sgrAddObject:[[CLGloble sharedInstance] objChange:key]];
            }
            [[NSUserDefaults standardUserDefaults] setObject:newArray
                                                      forKey:IfengOrderStore_orderStore];
            
            [[NSUserDefaults standardUserDefaults] synchronize];
            orderstor=newArray;

        }
    }
    return orderstor;

}

- (NSArray *)setOrderMenuItem:(NSArray *)itmes{
    NSArray *menuList=[self getOrderedMenuItem];
    if([menuList isEqualToArray:itmes]) return menuList;
    int n=[itmes count];
    NSMutableArray *storeItmes=[NSMutableArray arrayWithCapacity:n];
    for(int i=0;i<n;i++){
        NSString *pathId=[[itmes sgrGetDictionaryForIndex:i] sgrGetStringForKey:@"channelId"];
        if(_isStrNotNull(pathId) && ![storeItmes containsObject:pathId])
        [storeItmes addObject:pathId];
    }
    
    [self store:storeItmes];
    self.menuList=nil;
    return [self getOrderedMenuItem];
}

@end
